package initialize

import (
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver" // It's important
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/order_srv/global"
	"mxshop_srvs/order_srv/proto"
)

func InitConn() {

	conn, err := grpc.Dial(fmt.Sprintf("consul://%s:%d/%s?wait=14s&tag=%s", global.Server.Consul.Host, global.Server.Consul.Port, global.Server.GoodsSrvInfo.Name, global.Server.GoodsSrvInfo.Name),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)

	if err != nil {
		zap.S().Fatal("连接【商品服务】失败")
	}
	global.GoodsClient = proto.NewGoodsClient(conn)

	invConn, err := grpc.Dial(fmt.Sprintf("consul://%s:%d/%s?wait=14s&tag=%s", global.Server.Consul.Host, global.Server.Consul.Port, global.Server.InventorySrvInfo.Name, global.Server.InventorySrvInfo.Name),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)

	if err != nil {
		zap.S().Fatal("连接【库存服务】失败")
	}
	global.InventoryClient = proto.NewInventoryClient(invConn)
}
